package com.servlet;
import com.bean.Login;
import com.dao.LoginDao;
import com.util.DBHelper;

import org.apache.ibatis.session.SqlSession;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;


public class LoginServlet extends HttpServlet {

    private static final long serialVersionUID = 3879331499313257950L;


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        登录
        DBHelper DB=new DBHelper();
        SqlSession session = null;
        //连接不上数据库
        try {
            session = DB.session();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //接受前台传递的参数
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        LoginDao loginDao = session.getMapper(LoginDao.class);
        Login login = loginDao.Login(new Login(name, pwd));
        PrintWriter out=response.getWriter();
        if(login!=null){
            request.getSession().setAttribute("login",login);
            response.sendRedirect("jsp/public/board.jsp");
            out.print(1);
        } else{
/*            response.sendRedirect("jsp/public/login.jsp");
*/            out.print(0);
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp){
    }
}
